{include file="public/layout" /}
<body class="laytp-container">
<div class="layui-card">
	<div class="layui-card-body">
	  <div class="layui-fluid">
		<div class="layui-row layui-col-space15">
		  <div class="layui-card layui-col-md12">
					<div class="layui-card-header">
						{if !$info['id']}<i class="fa fa-plus"></i> 添加商品促销{else}<i class="fa fa-pencil"></i> 编辑商品促销{/if}
						<i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i>
					</div>
					<div class="layui-card-body" pad15>
						<div class="layui-form form-label-w6" lay-filter="">
							<input type="hidden" name="info[id]" value="{$info['id']}">
							<div class="layui-form-item">
								<label class="layui-form-label">促销类型：</label>
								<div class="layui-input-inline" style="width:800px">
									<input type="radio" name="info[type]" value="1" title="满额立减" {if !$info['id'] || $info['type']==1}checked{/if} lay-filter="cuxiao_type">
<!--									<input type="radio" name="info[type]" value="2" title="满额赠送" {if $info['type'] && $info['type']==2}checked{/if} lay-filter="cuxiao_type">-->
<!--									<input type="radio" name="info[type]" value="3" title="加价换购" {if $info['type'] && $info['type']==3}checked{/if} lay-filter="cuxiao_type">-->
									<input type="radio" name="info[type]" value="4" title="满额打折" {if $info['type'] && $info['type']==4}checked{/if} lay-filter="cuxiao_type">
<!--									<input type="radio" name="info[type]" value="5" title="满件打折" {if $info['type'] && $info['type']==5}checked{/if} lay-filter="cuxiao_type">-->
<!--									<input type="radio" name="info[type]" value="6" title="满件立减" {if $info['type'] && $info['type']==6}checked{/if} lay-filter="cuxiao_type">-->
								</div>
								<div class="layui-word-aux layui-clear"></div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">活动标识：</label>
								<div class="layui-input-inline" style="width:100px">
									<input type="text" name="info[tip]" value="{$info.tip}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">活动名称：</label>
								<div class="layui-input-inline" style="width:300px">
									<input type="text" name="info[name]" value="{$info.name}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
								</div>
								<div class="layui-word-aux layui-clear">如：购物满100元减20元，购物满100元赠送玫瑰花一束，购物满100元加10元换购玫瑰花一束等</div>
							</div>
							<div id="edit1" {if $info && $info['type']!=1}style="display:none;"{/if}>
								{include file="cuxiao/edit1" /}
							</div>
							<div id="edit2" {if !$info || $info['type']!=2}style="display:none;"{/if}>
								{include file="cuxiao/edit2" /}
							</div>
							  <div id="edit3" {if $info && $info['type']!=3}style="display:none;"{/if}>
								  {include file="cuxiao/edit3" /}
							  </div>
							<div id="edit4" {if $info && $info['type']!=4}style="display:none;"{/if}>
								{include file="cuxiao/edit4" /}
							</div>
							  <div id="edit5" {if $info && $info['type']!=5}style="display:none;"{/if}>
								  {include file="cuxiao/edit5" /}
							  </div>
							  <div id="edit6" {if $info && $info['type']!=6}style="display:none;"{/if}>
								{include file="cuxiao/edit6" /}
							  </div>

							<div class="layui-form-item">
								<label class="layui-form-label">适用范围：</label>
								<div class="layui-input-inline" style="width:500px">
									<input type="radio" name="info[fwtype]" value="0" {if $info['fwtype']==0}checked{/if} title="所有商品" lay-filter="fwtype"/>
									<input type="radio" name="info[fwtype]" value="1" {if $info['fwtype']==1}checked{/if} title="指定类目" lay-filter="fwtype"/>
									<input type="radio" name="info[fwtype]" value="2" {if $info['fwtype']==2}checked{/if} title="指定商品" lay-filter="fwtype"/>
								</div>
								<div class="fwtype1" style="clear:both;{if $info['fwtype']!=1}display:none{/if}">
									<label class="layui-form-label"></label>
									<div class="layui-input-inline" style="width:auto">
										<table id="setcategorydiv"  class="layui-table" style="width:500px">
											<thead>
											<tr>
												<th>分类ID</th>
												<th>分类名称</th>
												<th>操作</th>
											</tr>
											</thead>
											{if $categorydata}
											{foreach $categorydata as $k=>$ff}
											<tr class="categorylisttr"><td>{$ff.id}</td><td>{$ff.name}</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delCategory(this,{$ff.id})">删除</button></td></tr>
											{/foreach}
											{/if}
											<tr id="categoryaddtr">
												<td colspan="3"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseCategory()">添加</button></td>
											</tr>
										</table>
										<input type="hidden" name="info[categoryids]" value="{$info.categoryids}"/>
									</div>
								</div>
								<script>
								var chooseCategoryLayer;
								function showChooseCategory(){
									chooseCategoryLayer = layer.open({type:2,title:'选择商品分类',content:"{:url('GoodsCategory/choosecategory')}",area:['1000px','600px'],shadeClose:true});
								}
								function chooseCategory(fid,fname){
									layer.close(chooseCategoryLayer);
									var categoryids = [];
									var isadd = 0;
									$('.categorylisttr').each(function(){
										var thisfid = $(this).find('td:eq(0)').html();
										if(thisfid == fid){
											isadd = 1
											dialog('该分类已添加过了');
										}
										categoryids.push(thisfid)
									})
									if(isadd == 0){
										categoryids.push(fid)
										$("input[name='info[categoryids]']").val(categoryids.join(','));
										$('#categoryaddtr').before('<tr class="categorylisttr"><td>'+fid+'</td><td>'+fname+'</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delCategory(this,'+fid+')">删除</button></td></tr>');
									}
									console.log(categoryids)
								}
								function delCategory(obj,fid){
									$(obj).parent().parent().remove();
									var categoryids = [];
									$('.categorylisttr').each(function(){
										categoryids.push($(this).find('td:eq(0)').html())
									})
									$("input[name='info[categoryids]']").val(categoryids.join(','));
								}
								</script>
								<div class="fwtype2" style="clear:both;{if $info['fwtype']!=2}display:none{/if}">
									<label class="layui-form-label"></label>
									<div class="layui-input-inline" style="width:auto">
										<table id="setproductdiv"  class="layui-table" style="width:700px">
											<thead>
											<tr>
												<th>商品ID</th>
												<th>商品名称</th>
												<th style="{if $info['type']!=4 && $info['type']!=5}display:none{/if}" class="prozk">单独折扣</th>
												<th style="{if $info['type']!=6 && $info['type']!=5}display:none{/if}" class="pronum">单独最低件数</th>
												<th>操作</th>
											</tr>
											</thead>
											{if $productdata}
											{foreach $productdata as $k=>$ff}
											<tr class="productlisttr">
												<td>{$ff.id}</td>
												<td>{$ff.name}</td>
												<td style="{if $info['type']!=4 && $info['type']!=5}display:none{/if}" class="prozk">
													<input type="text" name="info[prozk][]" class="layui-input" placeholder="0.01~9.99" value="{$info['prozk'][$k]}"/>
												</td>
												<td style="{if $info['type']!=6 && $info['type']!=5}display:none{/if}" class="pronum">
													<input type="text" name="info[pronum][]" class="layui-input" placeholder="设置后如未达到不打折" value="{$info['pronum'][$k]}"/>
												</td>
												<td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct(this,{$ff.id})">删除</button></td>
											</tr>
											{/foreach}
											{/if}
											<tr id="productaddtr">
												<td colspan="3"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseProduct()">添加</button></td>
												<td colspan="2" style="{if $info['type']!=4 && $info['type']!=5 && $info['type']!=6}display:none{/if}" class="prozk"></td>
											</tr>
										</table>
										<input type="hidden" name="info[productids]" value="{$info.productids}"/>
										<div class="layui-word-aux prozk" style="{if $info['type']!=4 && $info['type']!=5}display:none{/if}">注：不单独设置商品折扣则按照统一的折扣计算</div>
									</div>
								</div>
								<script>
								var cxtype = '{$info.type}';
								var chooseProductLayer;
								function showChooseProduct(){
									chooseProductLayer = layer.open({type:2,title:'选择商品',content:"{:url('Goods/choosegoods')}/hidebid/1",area:['1000px','600px'],shadeClose:true});
								}
								function choosegoods(res){
									var product = res
									layer.close(chooseProductLayer);
									var productids = [];
									var isadd = 0;
									$('.productlisttr').each(function(){
										var thisfid = $(this).find('td:eq(0)').html();
										if(thisfid == product.goods_id){
											isadd = 1
											dialog('该商品已添加过了');
										}
										productids.push(thisfid)
									})
									if(isadd == 0){
										productids.push(product.goods_id)
										$("input[name='info[productids]']").val(productids.join(','));
										$('#productaddtr').before('<tr class="productlisttr"><td>'+product.goods_id+'</td><td>'+product.goods_name+'</td><td class="prozk" style="width:100px;'+(cxtype==4||cxtype==5?'':'display:none')+'"><input type="text" name="info[prozk][]" value="" class="layui-input" placeholder="0.01~9.99"/></td><td class="pronum" style="width:100px;'+(cxtype==6||cxtype==5?'':'display:none')+'"><input type="text" name="info[pronum][]" value="" class="layui-input" placeholder="设置后如未达到不打折"/></td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct(this,'+product.id+')">删除</button></td></tr>');
									}
									console.log(productids)
								}
								function delProduct(obj,fid){
									$(obj).parent().parent().remove();
									var productids = [];
									$('.productlisttr').each(function(){
										productids.push($(this).find('td:eq(0)').html())
									})
									$("input[name='info[productids]']").val(productids.join(','));
								}
								</script>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">参与条件：</label>
								<div class="layui-input-inline" style="width:600px">
									<input type="checkbox" name="info[gettj][]" value="-1" title="所有人" {if in_array('-1',$info['gettj'])}checked{/if}/>
									<!-- <input type="checkbox" name="info[gettj][]" value="0" title="关注用户" {if in_array('0',$info['gettj'])}checked{/if}/> -->
									{foreach $memberlevel as $v}
									<input type="checkbox" name="info[gettj][]" value="{$v.rank}" title="{$v.level_name}" {if in_array($v['id'],$info['gettj'])}checked{/if}/>
									{/foreach}
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">开始时间：</label>
								<div class="layui-input-inline">
									<input type="text" name="info[starttime]" autocomplete="off" value="{$info.starttime}" lay-verify="required" lay-verType="tips" class="layui-input" id="starttime">
								</div>
								<div class="layui-form-mid layui-word-aux">活动开始时间</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">结束时间：</label>
								<div class="layui-input-inline">
									<input type="text" name="info[endtime]" autocomplete="off" value="{$info.endtime}" lay-verify="required" lay-verType="tips" class="layui-input" id="endtime">
								</div>
								<div class="layui-form-mid layui-word-aux">活动结束时间</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">序号：</label>
								<div class="layui-input-inline">
									<input type="text" name="info[sort]" value="{$info.sort}" class="layui-input">
								</div>
								<div class="layui-form-mid layui-word-aux">用于排序,越大越靠前</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label"></label>
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit lay-filter="formsubmit">提 交</button>
								</div>
							</div>
						</div>
					</div>
				</div>
		</div>
	  </div>
	</div>
</div>
	<script>
	layui.form.on('radio(group_status)',function(data){
		if(data.value == 1){
			$('.group_status1').show();
		}else{
			$('.group_status1').hide();
		}
		
	})
	layui.form.on('radio(cuxiao_type)',function(data){
		cxtype = data.value;
		$('.prozk').hide();
		if(data.value==1){
			$("input[name='info[tip]']").val('满减');
			$("#edit1").show();
			$("#edit2").hide();
			$("#edit3").hide();
			$("#edit4").hide();
			$("#edit5").hide();
			$("#edit6").hide();
		}
		if(data.value == 2){
			$("input[name='info[tip]']").val('满赠');
			$("#edit1").hide();
			$("#edit2").show();
			$("#edit3").hide();
			$("#edit4").hide();
			$("#edit5").hide();
			$("#edit6").hide();
		}
		if(data.value == 3){
			$("input[name='info[tip]']").val('换购');
			$("#edit1").hide();
			$("#edit2").hide();
			$("#edit3").show();
			$("#edit4").hide();
			$("#edit5").hide();
			$("#edit6").hide();
		}
		if(data.value == 4){
			$("input[name='info[tip]']").val('打折');
			$("#edit1").hide();
			$("#edit2").hide();
			$("#edit3").hide();
			$("#edit4").show();
			$("#edit5").hide();
			$("#edit6").hide();
		}
		if(data.value == 5){
			$("input[name='info[tip]']").val('打折');
			$("#edit1").hide();
			$("#edit2").hide();
			$("#edit3").hide();
			$("#edit4").hide();
			$("#edit5").show();
			$("#edit6").hide();
		}
		if(data.value == 6){
			$("input[name='info[tip]']").val('立减');
			$("#edit1").hide();
			$("#edit2").hide();
			$("#edit3").hide();
			$("#edit4").hide();
			$("#edit5").hide();
			$("#edit6").show();
		}
		if(data.value == 0){
			$("input[name='info[tip]']").val('特价');
			$("#edit1").show();
			$("#edit2").hide();
			$("#edit3").hide();
			$("#edit4").hide();
			$("#edit5").hide();
			$("#edit6").hide();
		}
	});
	layui.form.on('radio(yxqtype)',function(data){
		if(data.value==1){
			$('#yxqtimeitem').show();
			$('#yxqdateitem').hide();
		}else{
			$('#yxqtimeitem').hide();
			$('#yxqdateitem').show();
		}
	})
	layui.form.on('radio(paygive)',function(data){
		if(data.value==1){
			$('#paygiveset').show();
		}else{
			$('#paygiveset').hide();
		}
	})
	layui.form.on('radio(fwtype)',function(data){
		if(data.value==1){
			$('.fwtype1').show();
			$('.fwtype2').hide();
		}else if(data.value==2){
			$('.fwtype1').hide();
			$('.fwtype2').show();
		}else{
			$('.fwtype1').hide();
			$('.fwtype2').hide();
		}
	})
	layui.laydate.render({ 
		elem: '#starttime'
		,type: 'datetime'
		,range: false,
		trigger: 'click'
	});
	layui.laydate.render({ 
		elem: '#endtime'
		,type: 'datetime'
		,range: false,
		trigger: 'click'
	});

	layui.form.on('submit(formsubmit)', function(obj){
		console.log(obj)
		var field = obj.field
		var index = layer.load();
		$.post("{:url('save')}",field,function(data){
			layer.close(index);
			dialog(data.msg,data.status);
			if(data.status == 1){
				setTimeout(function(){
					parent.layer.closeAll();
					parent.tableIns.reload()
				},1000)
			}
		})
	})
  </script>
  <script>
	  var chooseProductLayer2;
	  function showChooseProduct2(){
		  chooseProductLayer2 = layer.open({type:2,title:'选择商品',content:"{:url('Goods/choosegoods')}/type/2/hidebid/1",area:['1000px','600px'],shadeClose:true});
	  }
	  var productinfo;
	  function choosepro2(data){
		  layer.close(chooseProductLayer2);
		  productinfo = data
		  console.log(data);
		  showgg(data.guigedata,productinfo.gglist);
	  }
	  var showggLayer;
	  function showgg(specs,gglist){
		  console.log(gglist.length);
		  if(gglist.length <=1){
			  $("input[name='info[proid]']").val(productinfo.goods_id);
			  $("input[name='info[ggid]']").val(gglist[0].item_id);
			  $("#proid_name").html(productinfo.goods_name);
			  $("#ggid_name").html(gglist[0].key_name);
			  return ;
		  }
		  var len = specs.length;
		  var newlen = 1;
		  var h = new Array(len);
		  var rowspans = new Array(len);
		  var html = '<div style="margin:10px"><table id="ggvaldiv" class="layui-table"><thead><tr>';
		  for(var i=0;i<len;i++){
			  html+="<th>" + specs[i].title + "</th>";
			  var itemlen = specs[i].items.length;
			  if(itemlen<=0) { itemlen = 1 };
			  newlen*=itemlen;
			  h[i] = new Array(newlen);
			  for(var j=0;j<newlen;j++){
				  h[i][j] = new Array();
			  }
			  var l = specs[i].items.length;
			  rowspans[i] = 1;
			  for(j=i+1;j<len;j++){
				  rowspans[i]*= specs[j].items.length;
			  }
		  }
		  html += '<th>库存</th>';
		  html += '<th>市场价</th>';
		  html += '<th>销售价</th>';
		  html += '<th>操作</th>';
		  html += '</tr></thead>';

		  for(var m=0;m<len;m++){
			  var k = 0,kid = 0,n=0;
			  for(var j=0;j<newlen;j++){
				  var rowspan = rowspans[m];
				  if( j % rowspan==0){
					  h[m][j]={ k:specs[m].items[kid].k,title: specs[m].items[kid].title, html: "<td rowspan='" +rowspan + "'>"+ specs[m].items[kid].title+"</td>\r\n",id: specs[m].items[kid].id};
				  }else{
					  h[m][j]={ k:specs[m].items[kid].k,title:specs[m].items[kid].title, html: "",id: specs[m].items[kid].id};
				  }
				  n++;
				  if(n==rowspan){
					  kid++; if(kid>specs[m].items.length-1) { kid=0; }
					  n=0;
				  }
			  }
		  }
		  var hh = "";
		  for(var i=0;i<newlen;i++){
			  hh+="<tr>";
			  var ks = [];
			  var titles = [];
			  for(var j=0;j<len;j++){
				  hh+=h[j][i].html;
				  ks.push( h[j][i].k);
				  titles.push( h[j][i].title);
			  }
			  ks =ks.join(',');
			  titles =titles.join(',');
			  if(typeof(gglist[ks])!='undefined'){
				  var val = gglist[ks];
			  }else{
				  var val = { procode:'',market_price:'',cost_price:'',shop_price:'',weight:'',stock:'1000',pic:''};
			  }
			  hh += '<td>'+val.stock+'</td>';
			  hh += '<td>'+val.market_price+'</td>';
			  hh += '<td>'+val.shop_price+'</td>';
			  hh += '<td>';
			  hh += ' <button class="table-btn" onclick="selectgg('+val.item_id+',\''+val.key_name+'\')">选择</button>';
			  hh += '</td>';
			  hh += "</tr>";
		  }
		  html+=hh;
		  html+='</table></div>';
		  showggLayer = layer.open({type:1,title:productinfo.goods_name,content:html,area:['900px','600px'],shadeClose:true})
	  }
	  function selectgg(ggid,ggname){
		  layer.close(showggLayer);
		  $("input[name='info[proid]']").val(productinfo.goods_id);
		  $("input[name='info[ggid]']").val(ggid);
		  $("#proid_name").html(productinfo.goods_name);
		  $("#ggid_name").html(ggname);
	  }
  </script>
</body>
</html>